package com.apobates.forum.thrones.tag;

import com.apobates.forum.attention.ImageDirectoryEnum;
import com.apobates.forum.attention.core.UploadImagePathConvert;
import java.io.IOException;
import java.util.ResourceBundle;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import com.apobates.forum.utils.Commons;
import java.net.URL;

/**
 * 塑造/解码像册的图片路径
 *
 * @author xiaofanku
 * @since 20190831
 */
public class MoldImagePathTag extends SimpleTagSupport {
    private String path;
    //
    private final String imageBucketDomain;
    private final String uploadImageDirectName;
    
    public MoldImagePathTag() {
        ResourceBundle gisResource = ResourceBundle.getBundle("global");
        this.imageBucketDomain = gisResource.getString("img.bucket.domain");
        this.uploadImageDirectName = gisResource.getString("img.bucket.upload.direct");
    }
    
    public void setPath(String path) {
        this.path = path;
    }
    
    @Override
    public void doTag() throws JspException, IOException {
        if (!Commons.isNotBlank(path)) {
            getJspContext().getOut().print("/static/img/140x140.png");
            return;
        }
        UploadImagePathConvert ipc = new UploadImagePathConvert(imageBucketDomain, uploadImageDirectName);
        String imagePath = ipc.decode(path, ImageDirectoryEnum.DEFAT, null).map(URL::toString).orElse("/static/img/140x140.png");
        //
        getJspContext().getOut().print(imagePath);
    }
}